package com.ftg.product.exam.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 分页信息封装类，用来完成特定前台框架的数据返回格式
 *    这里主要为了layui封装了一套dto
 * @author KangJx
 * @date 2020/12/6
 */
@Data
public class ResultType<T> implements Serializable {

    private Integer code;
    private String msg;
    private List<T> data;
    private long count;
    private long page;
    private long limit;

    /**
     * 单独处理plus返回数据
     * @param ip
     */
    public ResultType(IPage<T> ip){
        this.setCode(0);
        this.setMsg("success");
        this.setData(ip.getRecords());
        this.setCount(ip.getTotal());
    }

    /**
     * 无参构造器
     */
    public ResultType() {
        super();
    }

    public ResultType(long page,long limit) {
       this.setPage(page);
       this.setLimit(limit);
    }

    public ResultType(Status status) {
        super();
        this.code = status.code;
        this.msg = status.msg;
    }

    public ResultType result(List<T> data) {
        this.data = data;
        return this;
    }

    public ResultType msg(String messmsgage) {
        this.msg = msg;
        return this;
    }
    public ResultType total(long total) {
        this.count = total;
        return this;
    }

    /**
     * 只返回状态，状态码，消息
     *
     * @param code
     */
    public ResultType(Integer code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    /**
     * 只返回状态，状态码，数据对象
     *
     * @param code
     */
    public ResultType(Integer code, List<T> data) {
        super();
        this.code = code;
        this.data = data;
    }

    /**
     * 返回全部信息即状态，状态码，消息，数据对象
     *
     * @param code
     * @param message
     * @param result
     */
    public ResultType(Integer code, String message, List<T> result) {
        super();
        this.code = code;
        this.msg = message;
        this.data = result;
    }
}

